/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			mainwindow.cc
  \date			Sep 2011
  \author		TNick

  \brief		Contains the implementation for MainWindow class


  Copyright (c) 2011, Nicu Tofan
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
 * Neither the name of the copyright holder nor the
   names of its contributors may be used to endorse or promote products
   derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL NICU TOFAN BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<main.h>

#include    <QStyleFactory>
#include    <QWhatsThis>
#include    <QSettings>
#include    <QMessageBox>
#include    <QCloseEvent>
#include    <QDir>
#include    <QFileDialog>
#include    <QCloseEvent>
#include    <QTreeView>
#include    <QDockWidget>
#include    <QGraphicsScene>

#include    <gui/mdichild.h>
#include    <gui/finddlg.h>

#include	"mainwindow.h"
#include	"ui_mainwindow.h"

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */


/* ------------------------------------------------------------------------- */
MainWindow::MainWindow                          (QWidget *parent) :
	QMainWindow(parent),
	ui(new Ui::MainWindow)
{
	g_MW = this;

	ui->setupUi(this);

	createDockWindows();

	c_bs_slot.initBasicSlots(ui);

	c_ui_tpl.initUiTemplates(ui);

	c_ssheet.initStyleSheets(ui);

	c_r_files.initRecentFiles(ui);

	c_sheet.initSheetSlots(ui);

	c_bs_mdi.initBasicMdi(ui);


	/* main panel position and size */
	QSettings settings;
	settings.beginGroup("MainWindow");

	QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
	QSize size = settings.value("size", QSize(400, 400)).toSize();
	move(pos);
	resize(size);
	settings.endGroup();


	/* debug */
	c_bs_slot.slot_New();

	ui->statusbar->showMessage(tr("Ready"));

#if		(ENABLE_DDHC == 1)
	QTreeView * tv_tree_ddhc = new QTreeView(this);
	tv_tree_ddhc->setHeaderHidden( false );
	tv_tree_ddhc->setRootIsDecorated( true );
	tv_tree_ddhc->setUniformRowHeights( true );
	tv_tree_ddhc->setMinimumWidth(550);
	tv_tree_ddhc->expandAll();
	DDHC::Model * ddhc_mdl =  DDHC::Model::mInst();
	tv_tree_ddhc->setModel(ddhc_mdl);
	ui->centralwidget->layout()->addWidget(tv_tree_ddhc);

	// let model know about the tree
	DDHC_SET_TREE_VIEW(tv_tree_ddhc);
#endif	//	ENABLE_DDHC
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
MainWindow::~MainWindow	( void )
{
	/* execution never trully reaches here ... */
	delete ui;
}
/* ========================================================================= */


/* ------------------------------------------------------------------------- */
bool        MainWindow::_open                   (QString s_file)
{
	QMdiSubWindow *existing = c_bs_mdi.findMdiChild(s_file);
	if (existing)
	{
		ui->mdiArea->setActiveSubWindow(existing);
		return false;
	}

	MdiChild *child = c_bs_mdi.createMdiChild();
	if (child->loadFile(s_file))
	{
		ui->statusbar->showMessage(tr("File loaded"), 2000);
		child->show();
		c_r_files.addRecentFile(s_file);
		return true;
	}
	else
	{
		child->close();
	}
	return false;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QMenu *			MainWindow::menuWin				( void )
{
	return ui->menuPanels;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
int				MainWindow::docsOpened			( void )
{
	return ui->mdiArea->subWindowList().count();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
MdiChild *		MainWindow::docByIndex			(int doc_idx)
{
	QList<QMdiSubWindow *> win_l = ui->mdiArea->subWindowList();
	if (win_l.count() <= doc_idx)
		return NULL;
	return static_cast<MdiChild *>(win_l[doc_idx]);
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			MainWindow::closeEvent          (QCloseEvent *event)
{
	c_bs_slot.closeEvent(event);
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			MainWindow::createDockWindows	( void )
{

	QDockWidget	* dock;
	QTreeView	* tree_l;

	dock = new QDockWidget(tr("Structure"), this);
	//dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
	tree_l = new QTreeView(dock);
	dock->setWidget(tree_l);
	addDockWidget(Qt::RightDockWidgetArea, dock);
	//viewMenu->addAction(dock->toggleViewAction());



	dock = new QDockWidget(tr("Tracer"), this);
	//dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
	QGraphicsScene * scene_l = new QGraphicsScene(dock);
	QGraphicsView * view_l = new QGraphicsView(dock);
	view_l->setScene(scene_l);
	dock->setWidget(view_l);
	addDockWidget(Qt::RightDockWidgetArea, dock);
	//viewMenu->addAction(dock->toggleViewAction());



	dock = new QDockWidget(tr("Find / Replace"), this);
	QWidget * top_lvl = new FindDlg(dock);
	//dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
	dock->setWidget(top_lvl);
	addDockWidget(Qt::RightDockWidgetArea, dock);
	//viewMenu->addAction(dock->toggleViewAction());




//    connect(customerList, SIGNAL(currentTextChanged(QString)),
//            this, SLOT(insertCustomer(QString)));
//    connect(paragraphsList, SIGNAL(currentTextChanged(QString)),
//            this, SLOT(addParagraph(QString)));
}
/* ========================================================================= */


/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
